﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.IRepository.SystemSettings.CommonlyUsedSettings;
using SystemCenterSoa.PoModel.SystemSettings.CommonlyUsedSettings;
using SystemCenterSoa.Repository.CommonLib;
using Dapper;
using System.Data;
using System.Data.SqlClient;
using SystemCenterSoa.PoModel.SystemSettings.InstituTions;

namespace SystemCenterSoa.Repository.SystemSettings.CommonlyUsedSettings
{
    public class IntroductionPersonnelSalaryRepository : BaseRepository, IIntroductionPersonnelSalaryRepository
    {
        public IntroductionPersonnelSalaryRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {

        }

        public object GetDropDown()
        {
            string sql = "select *from divisional_management";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                List<DepartmentOfManagementPo> list = conn.Query<DepartmentOfManagementPo>(sql).ToList();
                List<int> yearlist=new List<int> ();
                List<int> monthlist = new List<int>();
                int i;
                for (i=2015; i<=2050; i++)
                {
                    yearlist.Add(i);
                }
                for (i = 1; i <=12; i++)
                {
                    monthlist.Add(i);
                }
                return new { list, yearlist, monthlist };
            }

        }

        public int IntroductionPersonnelSalaryAdd(IntroductionPersonnelSalaryPo form)
        {
            _systemCenterDbSoaContext.Add(form);
            return _systemCenterDbSoaContext.SaveChanges();
        }
        public List<IntroductionPersonnelSalaryPo> SearchPage(string depname, int year, int month)
        {
            var list = _systemCenterDbSoaContext.IntroductionPersonnelSalaryPo.AsQueryable();
            if (!string.IsNullOrWhiteSpace(depname))
            {
                list = list.Where(c => c.dep_name == depname);
            }
            if (year > 0)
            {
                list = list.Where(c => c.issue_year == year);
            }
            if (month > 0)
            {
                list = list.Where(c => c.issue_month == month);
            }
            return list.ToList();
            //var a = _systemCenterDbSoaContext.IntroductionPersonnelSalaryPo.AsQueryable();
            //if (searchModel.SearchItem != null && searchModel.SearchItem.Count() > 0)
            //{
            //    Dictionary<string,string> tempDi = searchModel.SearchItem;

            //    string depname = "dep_name";
            //    if (tempDi.Keys.Contains(depname) && !string.IsNullOrEmpty(tempDi[depname]))
            //    {
            //        a = a.Where(c => c.dep_name == tempDi[depname]);
            //    }

            //    string issueyear = "issue_year";
            //    if (tempDi.Keys.Contains(issueyear) && !string.IsNullOrEmpty(tempDi[issueyear]))
            //    {
            //        int i = -1;
            //        if(int.TryParse(tempDi[issueyear],out i))
            //        {
            //        a = a.Where(c => c.issue_year == i);
            //        }                  
            //    }
            //    string issuemonth = "issue_month";
            //    if (tempDi.Keys.Contains(issuemonth) && !string.IsNullOrEmpty(tempDi[issuemonth]))
            //    {
            //        int i = -1;
            //        if (int.TryParse(tempDi[issuemonth], out i))
            //        {
            //            a = a.Where(c => c.issue_month == i);
            //        }
            //    }     
            //}
            //return BasePageSearch(a,searchModel); 
        }
    }
}
